package srcInterviewPrep;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

public final class BFS
{
    public static void RunBFS(Vertex start)
    {
        if (start == null)
        {
            throw new IllegalArgumentException("Parameter 'start' can not be null");
        }

        Deque<Vertex> nodesToVisit = new ArrayDeque<Vertex>();
        // nodesToVisit.add(start);
        nodesToVisit.push(start);

        Map<Vertex, Vertex> visitedNodes = new HashMap<Vertex, Vertex>();

        while (!nodesToVisit.isEmpty())
        {
            Vertex v = nodesToVisit.pop();

            System.out.println(v.Id());

            visitedNodes.put(v, v);

            for (Vertex n : v.Neighbors())
            {
                if (!visitedNodes.containsKey(n))
                {
                    // nodesToVisit.add(n);
                    nodesToVisit.push(n);
                }
            }
        }
    }
}
